/*
 * FeedExpress
 * (C) Copyright 2003 Jeppe Cramon (jeppe@cramon.dk)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Disclaimer:
 * -----------
 * This software is provided "as is" without warranty of any kind,
 * either expressed or implied. The entire risk as to the
 * quality and performance of the software is with you. Should the
 * software prove defective, you assume the cost of all necessary
 * servicing, repair, or correction. In no event shall the author,
 * copyright holder, or any other party who may redistribute the
 * software be liable to you for damages, including any general,
 * special, incidental, or consequential damages arising out of
 * the use or inability to use the software (including, but not
 * limited to, loss of data, data being rendered inaccurate, loss of
 * business profits, loss of business information, business
 * interruptions, loss sustained by you or third parties, or a
 * failure of the software to operate with any other software) even
 * if the author, copyright holder, or other party has been advised
 * of the possibility of such damages. 
 * 
 */

using System;
using System.Collections;	
using System.Windows.Forms;

namespace Pbdms.Common{
	/// <summary>
	/// This class is an implementation of the 'IComparer' interface, using a case insensitive comparer and
	/// a DateTime comparer for the DateColumn. Only supports one DateTime column at the moment, but more can be added
	/// by using a List.
	/// </summary>
	public class ListViewColumnSorter : IComparer {
		/// <summary>
		/// The column to be sorted
		/// </summary>
		private int mColumnToSort;
		/// <summary>
		/// Specified which column is a date column
		/// </summary>
		private int mDateColumn;
		/// <summary>
		/// The case insensitive comparer object
		/// </summary>
		private CaseInsensitiveComparer mObjectCompare;
		/// <summary>
		/// The order in which to sort, e.g. 'Ascending'
		/// </summary>
		private SortOrder mSortOrder;

		/// <summary>
		/// Class constructor.  Initializes various elements
		/// </summary>
		public ListViewColumnSorter() {
			// Default column is 0
			mColumnToSort = 0;
			//Default sort order is 'None'
			mSortOrder = SortOrder.None;
			// Init CaseInsensitiveComparer object
			mObjectCompare = new CaseInsensitiveComparer();
		}

		/// <summary>
		/// Compares the two objects passed using a case insensitive comparison for all other comlumns than
		/// the DateColumn.
		/// </summary>
		/// <param name="x">First object to be compared</param>
		/// <param name="y">Second object to be compared</param>
		/// <returns>Result of comparison: 0 if equal. Negative if "x" is less than "y". Positive if "x" is greater than "y"</returns>
		public int Compare(object x, object y) {
			int compareResult;
			ListViewItem listviewX, listviewY;
			listviewX = (ListViewItem)x;
			listviewY = (ListViewItem)y;

			// Get the value for the columns.
			// NB: Getting the value differs depending on wether it's the first (main) column or a sub column
			string listViewXValue, listViewYValue;
			if (mColumnToSort == 0) {
				listViewXValue = listviewX.Text;
				listViewYValue = listviewY.Text;
			} else {
				listViewXValue = listviewX.SubItems[mColumnToSort].Text;
				listViewYValue = listviewY.SubItems[mColumnToSort].Text;
			}

			// Compare the two items
			// Check if we're going to use the default comparer or our own date sorter
			if (mColumnToSort == mDateColumn) {
				compareResult = DateTime.Compare(DateTime.Parse(listViewXValue),
									DateTime.Parse(listViewYValue));

			} else {
				compareResult = mObjectCompare.Compare(listViewXValue,listViewYValue);
			}
			
			// Calculate return valuye
			if (mSortOrder == SortOrder.Ascending) {
				return compareResult;
			} else if (mSortOrder == SortOrder.Descending) {
				return -compareResult;
			} else {
				// Equality
				return 0;
			}
		}
    
		/// <summary>
		/// Sort order. E.g. "Ascending" or "Descending"
		/// </summary>
		public SortOrder Order {
			set {mSortOrder = value;}
			get {return mSortOrder;}
		}

		/// <summary>
		/// Gets or sets which column is the date column
		/// </summary>
		public int DateColumn {
			set {mDateColumn = value;}
			get {return mDateColumn;}
		}

		/// <summary>
		/// The number of the column to sort. Default value is "0"
		/// </summary>
		public int SortColumn {
			set {mColumnToSort = value;}
			get {return mColumnToSort;}
		}
	}
}